package cn.tedu._06article.base.interceptors;

import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.servlet.http.HttpSession;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.servlet.HandlerInterceptor;

@Slf4j
public class LoginCheckInterceptor implements HandlerInterceptor {
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        log.debug("开始处理请求：{} {}",request.getMethod(),request.getRequestURI());
        // 获取Session对象
        HttpSession session = request.getSession();
        // 从Session中获取用户数据
        Object user = session.getAttribute("user");
        if (user == null) {
            // 用户未登录
            throw new RuntimeException("请登录后再访问！");
        }
        return true;
    }
}
